/* eslint-disable no-unused-vars */
import { createRouter, createWebHistory } from 'vue-router';
import { getCurrentUser } from '@/api/user';
import MainHome from '../views/MainHome.vue';
import HomeView from '../views/HomeView.vue';
import AboutView from '../views/AboutView.vue';
import ServicesView from '../views/ServicesView.vue';
import SoftwareListView from '../views/SoftwareListView.vue';
import SoftwareDetail from '../views/SoftwareDetail.vue';
import ContactView from '../views/ContactView.vue';
import LoginView from '../views/LoginView.vue';
import RegisterView from '../views/RegisterView.vue';
import AdminHomeView from '../views/Admin/AdminHomeView.vue';
import UserManagement from '../views/Admin/UserManagement.vue';
import RolePermissionView from '../views/Admin/RolePermissionView.vue';
import SoftwareManagement from '../views/Admin/SoftwareManagement.vue';
import SystemLogView from '../views/Admin/SystemLogView.vue';

const routes = [
  // 管理区域路由
  {
    path: '/admin',
    name: 'Admin',
    component: () => import('../views/Admin/AdminView.vue'),
    meta: { requiresAdmin: true },
    redirect: '/admin/home',
    children: [
      {
        path: 'Home',
        name: 'AdminHome',
        component: AdminHomeView,
        meta: { requiresAdmin: true }
      },
      {
        path: 'users',
        name: 'UserManagement',
        component: UserManagement,
        meta: { requiresAdmin: true }
      },
      {
        path: 'roles',
        name: 'RolePermission',
        component: RolePermissionView,
        meta: { requiresAdmin: true }
      },
      {
        path: 'software',
        name: 'SoftwareManagement',
        component: SoftwareManagement,
        meta: { requiresAdmin: true }
      },
      {
        path: 'logs',
        name: 'SystemLog',
        component: SystemLogView,
        meta: { requiresAdmin: true }
      }
    ]
  },
  {
    path: '/',
    name: 'Home',
    component: MainHome,
    redirect: '/home',
    children: [
      {
        path: '/login',
        name: 'Login',
        component: LoginView
      },
      {
        path: '/register',
        name: 'Register',
        component: RegisterView
      },
      {
        path: '/home',
        name: 'MainHome',
        component: HomeView
      },
      {
        path: '/about',
        name: 'About',
        component: AboutView
      },
      {
        path: '/services',
        name: 'Services',
        component: ServicesView
      },
      {
        path: '/software',
        name: 'Software',
        component: SoftwareListView
      },
      {
        path: '/software/:id',
        name: 'SoftwareDetail',
        component: SoftwareDetail,
        props: true
      },
      {
        path: '/contact',
        name: 'Contact',
        component: ContactView
      }
    ]
  },
  // 默认路由，重定向到首页
  {
    path: '/:pathMatch(.*)*',
    redirect: '/'
  }
];

const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes
});

// 全局前置守卫
router.beforeEach(async (to, from, next) => {
  // 检查路由是否需要管理员权限
  if (to.meta.requiresAdmin) {
    try {
      // 获取当前用户信息
      const user = await getCurrentUser();
      // 检查用户角色
      if (user.role === 'admin') {
        next();
      } else {
        // 非管理员重定向到首页
        next('/');
      }
    } catch (error) {
      // 获取用户信息失败，重定向到登录页
      next('/login');
    }
  } else {
    // 不需要管理员权限的路由直接通过
    next();
  }
});

export default router;